"""
@Time : 2024/6/17 下午4:33
@Auth : AmirKD
@IDE : PyCharm
"""
import tkinter as tk
from datetime import datetime

from api.sparkAPI import content, main
from modules.Add_Project_Info import Add_Project_Info, Info
from modules.Del_Project_Info import Del_Project_Info
from modules.Mod_Project_Info import Mod_Project_Info
from modules.Mod_Project_Info_judge import Mod_Project_Info_judge
from modules.Print_Project_Info import Print_Project_Info
from modules.Search_Project_Info import Search_Project_Info
from util import util
from tkinter import ttk


class vo:
    # window = None
    def __init__(self, root):
        self.root = root

    # 添加项目信息的窗口
    def Window_Add(self):
        # root = tk.Tk()
        # 实例化对象，创建root的子窗口window
        window = tk.Toplevel(self.root)
        # 窗口名字
        window.title("添加项目信息")
        # 窗口大小
        window.geometry('500x500')

        # 关于序号的 label 和 entry
        Txt_ID = tk.StringVar()
        Txt_ID.set("")
        tk.Label(window, text="序号 (数字)：", font=('Microsoft YaHei', 10), width=15).place(x=75, y=50, anchor='nw')
        Entry_Line1 = tk.Entry(window, font=('Microsoft YaHei', 15), textvariable=Txt_ID, width=20)
        Entry_Line1.place(x=200, y=50, anchor='nw')

        # 关于项目名称的 label 和 entry
        Txt_Name = tk.StringVar()
        Txt_Name.set("")
        tk.Label(window, text="项 目 名 称：", font=('Microsoft YaHei', 10), width=15).place(x=75, y=100, anchor='nw')
        Entry_Line2 = tk.Entry(window, font=('Microsoft YaHei', 15), textvariable=Txt_Name, width=20)
        Entry_Line2.place(x=200, y=100, anchor='nw')

        # 关于项目内容的 label 和 entry
        Txt_Project = tk.StringVar()
        Txt_Project.set("")
        tk.Label(window, text="项 目 内 容：", font=('Microsoft YaHei', 10), width=15).place(x=75, y=150, anchor='nw')
        Entry_Line3 = tk.Entry(window, font=('Microsoft YaHei', 15), textvariable=Txt_Project, width=20)
        Entry_Line3.place(x=200, y=150, anchor='nw')

        # 获取当前时间
        time_now = datetime.now()

        # 关于项目创建时间的 label 和 entry
        Txt_creat_time = tk.StringVar()
        Txt_creat_time.set("")
        tk.Label(window, text="项目创建时间：", font=('Microsoft YaHei', 10), width=15).place(x=75, y=200, anchor='nw')
        Entry_Line4 = tk.Entry(window, font=('Microsoft YaHei', 15), textvariable=Txt_creat_time, width=20)
        # 项目创建时间默认为当天
        Entry_Line4.insert(0, f"{time_now.year}/{time_now.month}/{time_now.day}")
        Entry_Line4.place(x=200, y=200, anchor='nw')

        # 关于项目截止时间的 label 和 entry
        Txt_finish_time = tk.StringVar()
        Txt_finish_time.set("")
        tk.Label(window, text="项目截止时间：", font=('Microsoft YaHei', 10), width=15).place(x=75, y=250, anchor='nw')
        Entry_Line5 = tk.Entry(window, font=('Microsoft YaHei', 15), textvariable=Txt_finish_time, width=20)
        # 项目截止时间默认为当天
        Entry_Line5.insert(0, f"{time_now.year}/{time_now.month}/{time_now.day}")
        Entry_Line5.place(x=200, y=250, anchor='nw')

        # 关于项目完成进度的 label 和 entry
        progress = tk.StringVar()
        progress.set("")
        tk.Label(window, text="项目完成进度：", font=('Microsoft YaHei', 10), width=15).place(x=75, y=300, anchor='nw')
        Entry_Line6 = tk.Entry(window, font=('Microsoft YaHei', 15), textvariable=progress, width=20)
        # 进度默认为0%
        Entry_Line6.insert(0, "0%")
        Entry_Line6.place(x=200, y=300, anchor='nw')

        # 关于"确认"组件，此处绑定函数Add_Project_Info用于添加项目信息
        Button1_Yes = tk.Button(window, text='确认', bg='silver', font=('Microsoft YaHei', 12),
                                command=lambda: Add_Project_Info(Txt_ID.get(), Txt_Name.get(), Txt_Project.get(),
                                                                 Txt_creat_time.get(), Txt_finish_time.get(),
                                                                 progress.get(), window),
                                width=10)
        Button1_Yes.place(x=75, y=400, anchor='nw')

        # 关于"取消"组件，此处绑定函数destroy()用于关闭窗口
        Button2_No = tk.Button(window, text='取消', bg='silver', font=('Microsoft YaHei', 12),
                               command=lambda: window.destroy(),
                               width=10)
        Button2_No.place(x=325, y=400, anchor='nw')

        # 窗口显示
        window.mainloop()

    # 删除项目信息的窗口
    def Window_Del(self):
        # 创建root的子窗口
        window = tk.Toplevel(self.root)
        window.title("删除项目信息")
        window.geometry('500x300')

        # 关于序号的 label 和 entry
        Txt_ID = tk.StringVar()
        Txt_ID.set("")
        tk.Label(window, text="序号 (数字)：", font=('Microsoft YaHei', 10), width=15).place(x=75, y=100, anchor='nw')
        Entry_Line1 = tk.Entry(window, font=('Microsoft YaHei', 15), textvariable=Txt_ID, width=20)
        Entry_Line1.place(x=200, y=100, anchor='nw')

        # 关于"确认"组件，此处绑定函数Del_Project_Info用于删除项目信息
        Button1_Yes = tk.Button(window, text='确认', bg='silver', font=('Microsoft YaHei', 12),
                                command=lambda: Del_Project_Info(Txt_ID.get(), window), width=10)
        Button1_Yes.place(x=75, y=200, anchor='nw')

        # 关于"取消"组件，此处绑定函数destroy()用于关闭窗口
        Button2_No = tk.Button(window, text='取消', bg='silver', font=('Microsoft YaHei', 12),
                               command=lambda: window.destroy(),
                               width=10)
        Button2_No.place(x=325, y=200, anchor='nw')

        # tk.StringVar()用于接收用户输入
        result = tk.StringVar()
        result.set("       请先通过'查询项目信息'查询待删除项目的序号")

        # 在界面中显示文本框,打印result的信息
        Show_result = tk.Label(window, fg="red", font=("Microsoft YaHei", 12), bd='0', anchor='nw', textvariable=result)
        Show_result.place(x="50", y="50", width="400", height="50")

        # 显示窗口
        window.mainloop()

    # 修改项目信息的窗口_序号
    def Window_Mod(self):
        # 创建root的子窗口
        window = tk.Toplevel(self.root)
        window.title("修改项目信息")
        window.geometry('500x300')

        # 关于序号的 label 和 entry
        Txt_ID = tk.StringVar()
        Txt_ID.set("")
        tk.Label(window, text="序号 (数字)：", font=('Microsoft YaHei', 10), width=15).place(x=75, y=100, anchor='nw')
        Entry_Line1 = tk.Entry(window, font=('Microsoft YaHei', 15), textvariable=Txt_ID, width=20)
        Entry_Line1.place(x=200, y=100, anchor='nw')

        # 关于"确认"组件，此处绑定函数Mod_Project_Info用于修改项目信息
        Button1_Yes = tk.Button(window, text='确认', bg='silver', font=('Microsoft YaHei', 12),
                                command=lambda: Mod_Project_Info_judge(Txt_ID.get()), width=10)
        Button1_Yes.place(x=75, y=200, anchor='nw')

        # 关于"取消"组件，此处绑定函数destroy()用于关闭窗口
        Button2_No = tk.Button(window, text='取消', bg='silver', font=('Microsoft YaHei', 12),
                               command=lambda: window.destroy(), width=10)
        Button2_No.place(x=325, y=200, anchor='nw')

        # 在界面中显示文本框,打印result的信息
        result = tk.StringVar()
        result.set("     请先通过'查询项目信息'查询待修改项目的序号")
        Show_result = tk.Label(window, fg="red", font=("Microsoft YaHei", 12), bd='0', anchor='nw',
                               textvariable=result)
        Show_result.place(x="50", y="50", width="400", height="50")

        # 显示窗口
        window.mainloop()

    # 修改项目信息的窗口_其他
    def Window_Mod_Input(self, ID):
        # 创建root的子窗口
        window = tk.Toplevel()
        window.title("修改项目信息")
        window.geometry('500x500')

        # 关于项目名称的 label 和 entry
        Txt_Name = tk.StringVar()
        Txt_Name.set("")
        tk.Label(window, text="项 目 名 称：", font=('Microsoft YaHei', 10), width=15).place(x=75, y=100, anchor='nw')
        Entry_Line2 = tk.Entry(window, font=('Microsoft YaHei', 15), textvariable=Txt_Name, width=20)
        Entry_Line2.place(x=200, y=100, anchor='nw')

        # 关于项目内容的 label 和 entry
        Txt_project = tk.StringVar()
        Txt_project.set("")
        tk.Label(window, text="项 目 内 容：", font=('Microsoft YaHei', 10), width=15).place(x=75, y=150, anchor='nw')
        Entry_Line3 = tk.Entry(window, font=('Microsoft YaHei', 15), textvariable=Txt_project, width=20)
        Entry_Line3.place(x=200, y=150, anchor='nw')

        # 获取当前时间
        time_now = datetime.now()

        # 关于项目创建时间的 label 和 entry
        Txt_creat_time = tk.StringVar()
        Txt_creat_time.set("")
        tk.Label(window, text="项目创建时间：", font=('Microsoft YaHei', 10), width=15).place(x=75, y=200, anchor='nw')
        Entry_Line4 = tk.Entry(window, font=('Microsoft YaHei', 15), textvariable=Txt_creat_time, width=20)
        # 项目创建时间默认为当天
        Entry_Line4.insert(0, f"{time_now.year}/{time_now.month}/{time_now.day}")
        Entry_Line4.place(x=200, y=200, anchor='nw')

        # 关于项目截止时间的 label 和 entry
        Txt_finish_time = tk.StringVar()
        Txt_finish_time.set("")
        tk.Label(window, text="项目截止时间：", font=('Microsoft YaHei', 10), width=15).place(x=75, y=250, anchor='nw')
        Entry_Line5 = tk.Entry(window, font=('Microsoft YaHei', 15), textvariable=Txt_finish_time, width=20)
        Entry_Line5.insert(0, f"{time_now.year}/{time_now.month}/{time_now.day}")
        Entry_Line5.place(x=200, y=250, anchor='nw')

        # 关于项目完成进度的 label 和 entry
        Txt_progress = tk.StringVar()
        Txt_progress.set("")
        tk.Label(window, text="项目完成进度：", font=('Microsoft YaHei', 10), width=15).place(x=75, y=300, anchor='nw')
        Entry_Line6 = tk.Entry(window, font=('Microsoft YaHei', 15), textvariable=Txt_progress, width=20)
        # 进度默认为100%
        Entry_Line6.insert(0, "100%")
        Entry_Line6.place(x=200, y=300, anchor='nw')

        # 关于"确认"组件，此处绑定函数Mod_Project_Info_1用于修改项目信息
        Button1_Yes = tk.Button(window, text='确认', bg='silver', font=('Microsoft YaHei', 12),
                                command=lambda: Mod_Project_Info(ID, Txt_Name.get(), Txt_project.get(),
                                                                 Txt_creat_time.get(),
                                                                 Txt_finish_time.get(), Txt_progress.get(), window),
                                width=10)
        Button1_Yes.place(x=75, y=400, anchor='nw')

        # 关于"取消"组件，此处绑定函数destroy()用于关闭窗口
        Button2_No = tk.Button(window, text='取消', bg='silver', font=('Microsoft YaHei', 12),
                               command=lambda: window.destroy(),
                               width=10)
        Button2_No.place(x=325, y=400, anchor='nw')

        # 在界面中显示文本框,打印result的信息
        result = tk.StringVar()
        result.set("请输入修改后的信息")
        Show_result = tk.Label(window, fg="red", font=("Microsoft YaHei", 12), bd='0', anchor='nw', textvariable=result)
        Show_result.place(x="50", y="50", width="400", height="50")

        # 显示窗口
        window.mainloop()

    # 查找项目信息的窗口
    def Window_Ser(self):
        # 创建root的子窗口
        window = tk.Toplevel(self.root)
        window.title("查询项目信息")
        window.geometry('500x500')
        tk.Label(window, text="\t请输入部分相关信息", font=("Microsoft YaHei", 15), fg="red").place(x=75, y=20,
                                                                                                    anchor='nw')

        # 关于序号的 label 和 entry
        Txt_ID = tk.StringVar()
        Txt_ID.set("")
        tk.Label(window, text="序号 (数字)：", font=('Microsoft YaHei', 10), width=15).place(x=75, y=100, anchor='nw')
        Entry_Line1 = tk.Entry(window, font=('Microsoft YaHei', 15), textvariable=Txt_ID, width=20)
        Entry_Line1.place(x=200, y=100, anchor='nw')

        # 关于项目名称的 label 和 entry
        Txt_Name = tk.StringVar()
        Txt_Name.set("")
        tk.Label(window, text="项 目 名 称：", font=('Microsoft YaHei', 10), width=15).place(x=75, y=150, anchor='nw')
        Entry_Line2 = tk.Entry(window, font=('Microsoft YaHei', 15), textvariable=Txt_Name, width=20)
        Entry_Line2.place(x=200, y=150, anchor='nw')

        # 关于项目内容的 label 和 entry
        Txt_project = tk.StringVar()
        Txt_project.set("")
        tk.Label(window, text="项 目 内 容：", font=('Microsoft YaHei', 10), width=15).place(x=75, y=200, anchor='nw')
        Entry_Line3 = tk.Entry(window, font=('Microsoft YaHei', 15), textvariable=Txt_project, width=20)
        Entry_Line3.place(x=200, y=200, anchor='nw')

        # 关于项目创建时间的 label 和 entry
        Txt_creat_time = tk.StringVar()
        Txt_creat_time.set("")
        tk.Label(window, text="项目创建时间：", font=('Microsoft YaHei', 10), width=15).place(x=75, y=250, anchor='nw')
        Entry_Line4 = tk.Entry(window, font=('Microsoft YaHei', 15), textvariable=Txt_creat_time, width=20)
        Entry_Line4.place(x=200, y=250, anchor='nw')

        # 关于项目截止时间的 label 和 entry
        Txt_finish_time = tk.StringVar()
        Txt_finish_time.set("")
        tk.Label(window, text="项目截止时间：", font=('Microsoft YaHei', 10), width=15).place(x=75, y=300,
                                                                                             anchor='nw')
        Entry_Line5 = tk.Entry(window, font=('Microsoft YaHei', 15), textvariable=Txt_finish_time, width=20)
        Entry_Line5.place(x=200, y=300, anchor='nw')

        # 关于项目完成进度的 label 和 entry
        Txt_progress = tk.StringVar()
        Txt_progress.set("")
        tk.Label(window, text="项目完成进度：", font=('Microsoft YaHei', 10), width=15).place(x=75, y=350,
                                                                                             anchor='nw')
        Entry_Line6 = tk.Entry(window, font=('Microsoft YaHei', 15), textvariable=Txt_progress, width=20)
        Entry_Line6.place(x=200, y=350, anchor='nw')

        # 关于"确认"组件，此处绑定函数Search_Project_Info用于修改项目信息
        Button1_Yes = tk.Button(window, text='确认', bg='silver', font=('Microsoft YaHei', 12),
                                command=lambda: Search_Project_Info(Txt_ID.get(), Txt_Name.get(), Txt_project.get(),
                                                                    Txt_creat_time.get(), Txt_finish_time.get(),
                                                                    Txt_progress.get()),
                                width=10)
        Button1_Yes.place(x=75, y=400, anchor='nw')

        # 关于"取消"组件，此处绑定函数destroy()用于关闭窗口
        Button2_No = tk.Button(window, text='取消', bg='silver', font=('Microsoft YaHei', 12),
                               command=lambda: window.destroy(),
                               width=10)
        Button2_No.place(x=325, y=400, anchor='nw')

        # # 获取输入框的值
        # id_value = Txt_ID.get()
        # name_value = Txt_Name.get()
        # project_value = Txt_project.get()
        # create_time_value = Txt_creat_time.get()
        # finish_time_value = Txt_finish_time.get()
        # progress_value = Txt_progress.get()

        # 执行搜索逻辑，这里假设Search_Project_Info接受参数并返回结果字符串
        # search_result = Search_Project_Info(id_value, name_value, project_value, create_time_value, finish_time_value,
        #                                     progress_value)

        # 更新结果标签的文本
        # window.set(search_result)

        # # 在界面中显示文本框,打印result的信息
        # result = tk.StringVar()
        # result.set("      请输入待查找项目的部分信息(可不全填)")
        # Show_result = tk.Label(window, fg="red", font=("Microsoft YaHei", 12), bd='0', anchor='nw',
        #                        textvariable=result)
        # Show_result.place(x="50", y="50", width="400", height="50")
        #
        # # 显示窗口
        # window.mainloop()

    # 显示全部项目信息
    def Window_Show(self):
        window = tk.Toplevel(self.root)
        # tree = ttk.Treeview(window)
        window.title("显示全部项目信息")
        window.geometry('1500x500')
        result = tk.StringVar()
        result.set(Print_Project_Info(Info, window))
        # Show_result = tk.Label(window, fg="black", font=("Microsoft YaHei", 12), bd='0', anchor='nw',
        #                        textvariable=result)
        # Show_result.place(x="50", y="50")


    def Window_Api(self):
        window = tk.Toplevel(self.root)
        window.title("项目精灵")
        window.geometry('700x600')

        # 关于询问内容的 label 和 entry
        Txt_Input = tk.StringVar()
        Txt_Input.set("")
        tk.Label(window, text="请输入想要询问的内容:", font=("Microsoft YaHei", 12), fg="black").place(x=125, y=20,
                                                                                                       anchor='nw')
        Entry_Line1 = tk.Entry(window, font=('Microsoft YaHei', 13), textvariable=Txt_Input, width=33)
        Entry_Line1.place(x=300, y=23, anchor='nw')

        # 关于"确认"组件，此处绑定函数Add_Project_Info用于添加项目信息
        # Button1_ask = tk.Button(window, text='询问', bg='silver', font=('Microsoft YaHei', 12),
        #                         command=main(
        #                             appid="46e9193c",
        #                             api_secret="ZTQ3YzM0NGUxMmQwMWRmNjNjMzg0YzIw",
        #                             api_key="a6d97c04c658f312cb7e814d8fac8b37",
        #                             Spark_url="ws://spark-api.xf-yun.com/v1.1/chat",  # Lite环境的地址
        #                             domain="general",  # Lite版本址
        #                             query="需要我干什么"
        #                         ), width=20)
        Button1_ask = tk.Button(window, text='询问', bg='silver', font=('Microsoft YaHei', 12),
                                command=lambda: main(
                                    appid="46e9193c",
                                    api_secret="ZTQ3YzM0NGUxMmQwMWRmNjNjMzg0YzIw",
                                    api_key="a6d97c04c658f312cb7e814d8fac8b37",
                                    Spark_url="ws://spark-openapi.cn-huabei-1.xf-yun.com/v1/assistants/zlylc7lpki4b_v1",  # Lite环境的地址
                                    domain="general",  # Lite版本址
                                    query=Txt_Input.get(),
                                    window=window
                                ), width=20)
        Button1_ask.place(x=225, y=75, anchor='nw')

    # 输出返回的内容
    #     result = tk.StringVar()
    #     result.set(content + "end")
    #     Show_result = tk.Label(window, fg="black", bg="white", font=("Microsoft YaHei", 12), bd='0', anchor='nw',
    #                            textvariable=result)
    #     Show_result.place(x=200, y=300, width=520, height=500)
    #
    #     print()

    # 退出管理系统的窗口
    def Window_Exit(self):
        # 创建root的子窗口
        window = tk.Toplevel(self.root)
        window.title("退出管理系统")
        window.geometry('400x300')

        # 关于"确认"组件，此处绑定函数destroy()用于关闭主窗口
        Button1_Yes = tk.Button(window, text='确认', bg='silver', font=('Microsoft YaHei', 12),
                                command=lambda: self.root.destroy(), width=10)
        Button1_Yes.place(x=50, y=200, anchor='nw')

        # 关于"取消"组件，此处绑定函数destroy()用于关闭窗口
        Button2_No = tk.Button(window, text='取消', bg='silver', font=('Microsoft YaHei', 12),
                               command=lambda: window.destroy(),
                               width=10)
        Button2_No.place(x=250, y=200, anchor='nw')

        # 在界面中显示文本框,打印result的信息
        result = tk.StringVar()
        result.set("          您确认离开系统吗？")
        Show_result = tk.Label(window, fg="red", font=("Microsoft YaHei", 15), bd='0', anchor='nw', textvariable=result)
        Show_result.place(x="50", y="75", width="300", height="50")

        # 显示窗口
        window.mainloop()

    try:
        for i in util().ReadTxt():
            Info.append(i)
    except:
        pass
